package action;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;

import po.Address;
import po.User;

import bean.AddressDb;
import bo.HibernateSessionFactory;

public class AddressListAction {
	private String flag;
	private int friend = 0;
	private int relation = 0;
	private int colleague = 0;
	private int netfriend = 0;
	private int common = 0;
	private int other = 0; 
	private int total = 0;
	private int page;
	private int allpage;
	
	private List list;
	private List lt;
	
	private String call; 
	private String sign;
	private String alarm;
	
	public String execute(){
			HttpServletRequest request = ServletActionContext.getRequest();
			AddressDb aa = new AddressDb();		
			int pages = aa.getAddress(flag,(Integer)request.getSession().getAttribute("id"));
			allpage = (pages+2-1)/2;
			ServletActionContext.getRequest().setAttribute("allpage", allpage);
			this.setList(new AddressDb().getAddressone(flag, page,(Integer)request.getSession().getAttribute("id")));
			
			
			if(flag.equals("f")){
				this.setCall("朋友");
				this.setSign("f");
			}
			if(flag.equals("r")){
				this.setCall("亲人");
				this.setSign("r");
			}
			if(flag.equals("co")){
				this.setCall("同事");
				this.setSign("co");
			}
			if(flag.equals("n")){
				this.setCall("网友");
				this.setSign("n");
			}
			if(flag.equals("cm")){
				this.setCall("常用联系人");
				this.setSign("cm");
			}
			if(flag.equals("o")){
				this.setCall("其他");
				this.setSign("o");
			}
			
			Session session = HibernateSessionFactory.getSession();
			User u = (User)session.get(User.class, (Integer)request.getSession().getAttribute("id"));
				Set set = u.getAddresses();
				Iterator it = set.iterator();
				while(it.hasNext()){
					Address a = (Address)it.next();
				if(a.getBelong().equals("朋友")){
					friend++;
				}
				if(a.getBelong().equals("亲人")){
					relation++;
				}
				if(a.getBelong().equals("同事")){
					colleague++;
				}
				if(a.getBelong().equals("网友")){
					netfriend++;
				}
				if(a.getBelong().equals("常用联系人")){
					common++;
				}
				if(a.getBelong().equals("其他")){
					other++;
				}
			}
			total =friend+relation+colleague+netfriend+common+other;
			this.setTotal(total);
			this.setColleague(colleague);
			this.setCommon(common);
			this.setFriend(friend);
			this.setNetfriend(netfriend);
			this.setRelation(relation);
			this.setOther(other);
			this.setAlarm(alarm);
			session.close();
		return "addresslistf";
	}

	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public List getLt() {
		return lt;
	}
	public void setLt(List lt) {
		this.lt = lt;
	}

	public int getFriend() {
		return friend;
	}

	public void setFriend(int friend) {
		this.friend = friend;
	}

	public int getRelation() {
		return relation;
	}

	public void setRelation(int relation) {
		this.relation = relation;
	}

	public int getColleague() {
		return colleague;
	}

	public void setColleague(int colleague) {
		this.colleague = colleague;
	}

	public int getNetfriend() {
		return netfriend;
	}

	public void setNetfriend(int netfriend) {
		this.netfriend = netfriend;
	}

	public int getCommon() {
		return common;
	}

	public void setCommon(int common) {
		this.common = common;
	}

	public int getOther() {
		return other;
	}

	public void setOther(int other) {
		this.other = other;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getCall() {
		return call;
	}

	public void setCall(String call) {
		this.call = call;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getAllpage() {
		return allpage;
	}

	public void setAllpage(int allpage) {
		this.allpage = allpage;
	}

	public String getAlarm() {
		return alarm;
	}

	public void setAlarm(String alarm) {
		this.alarm = alarm;
	}



}
